$(function () {
    var users = [];

    getDataList();

    $('#submit-button').click(function () {
        $('#search-form [name="pageNumber"]').val(0);
        getDataList();
    });

    $('#pagination-bar').on('pageChange', function (e, page) {
        $('#search-form [name="pageNumber"]').val(page - 1);
        getDataList();
    });

    $('#data-list-group table tbody').on('click', 'tr', function () {
        var data = null;
        var id = $(this).data('id') + '';
        for (var i = 0, count = users.length; i < count; i++) {
            if (users[i].id + '' === id) {
                data = users[i];
                break;
            }
        }
        if (!data) {
            window.showInformationAlert(1, '用户信息获取失败。');
        } else {
            showDataModal(data);
        }
    });

    $('#data-modal .save-button').click(function () {
        var $saveButton = $(this);
        if ($saveButton.hasClass('loading')) return;

        var requestData = window.readFormData($('#extra-form'), true);
        window.showTab(1);
        window.loadingButton($saveButton, true);
        window.setModalClosable($('#data-modal'), false);
        $('#data-modal .modal-body .alert').alert('close');
        $.post('/api/user/' + requestData.id, requestData, function (data) {
            window.loadingButton($saveButton, false);
            window.setModalClosable($('#data-modal'), true);
            if (data.code === 200) {
                $('#data-modal').one('hidden.bs.modal', function () {
                    getDataList();
                });
                $('#data-modal').modal('hide');
            } else {
                var $alert = window.createAlert('alert-danger', data.message);
                $('#data-modal .modal-body').append($alert);
            }
        });
    });

    $('#super-user-input').click(function (e) {
        e.preventDefault();
    });

    $('#active-input').click(function (e) {
        if ($('#super-user-input').prop('checked')) {
            e.preventDefault();
        }
    });

    $('.link-input-group .input-group-append .btn').click(function () {
        var link = $(this).closest('.link-input-group').find('.data-item').val();
        if (link) {
            var $a = $('<a target="_blank" style="visibility:hidden">temp</a>');
            $a.prop('href', link);
            $('body').append($a);
            $a[0].click();
            $a.remove();
        }
    });

    function getDataList() {
        var requestData = window.readFormData($('#search-form'), true);
        window.showLoadingBox('数据加载中...');
        $.get('/api/users', requestData, function (data) {
            window.hideLoadingBox(function () {
                users = data.code === 200 ? data.data.list : [];
                window.showTableData(data, getDataList);
            });
        });
    }

    function showDataModal(data) {
        window.showFormData($('#basic-form'), data);
        window.showFormData($('#extra-form'), data);
        window.loadingButton($('#data-modal .save-button'), false);
        window.setModalClosable($('#data-modal'), true);
        $('#data-modal .modal-body .alert').alert('close');
        $('#data-modal').modal({backdrop: 'static', keyboard: false});

    }
});
